import Vue from 'vue'
import App from './App'
import store from './store'
import mixin from './mixin/mixin'

import '@/common/js/verifycation'
import config from '@/common/js/config'
import Util from '@/common/js/util'
import Pay from '@/common/js/pay'
import CheckLogin from '@/common/js/verification-login'
import Api from './api/index'
import deepClone from '@/utils/deepClone.js'

import uView from "uview-ui";
Vue.use(uView);

Vue.config.productionTip = false


Vue.mixin({
	methods: {
		setData: function(obj, callback) {
			let that = this;
			let keys = [];
			let val, data;
			Object.keys(obj).forEach(function(key) {
				keys = key.split('.');
				val = obj[key];
				data = that.$data;
				keys.forEach(function(key2, index) {
					if (index + 1 == keys.length) {
						that.$set(data, key2, val);
					} else {
						if (!data[key2]) {
							that.$set(data, key2, {});
						}
					}
					data = data[key2];
				})
			});
			callback && callback();
		} 
	}
});


App.mpType = 'app'

// app通用配置
Vue.prototype.$g_pay = Pay
// app通用配置
Vue.prototype.$g_config = config
// 工具函数库
Vue.prototype.$g_util = Util
// 验证登录
Vue.prototype.$g_checkLogin = CheckLogin
// API
Vue.prototype.$g_api = Api
Vue.prototype.$deepClone = deepClone

Vue.mixin(mixin)

Vue.filter('concatRequestUrl', concatRequestUrl)

const app = new Vue({
	store,
    ...App
})
app.$mount()

Date.prototype.Format = function(fmt) {
	var o = {
		"M+": this.getMonth() + 1, //月份
		"d+": this.getDate(), //日
		"h+": this.getHours(), //小时
		"m+": this.getMinutes(), //分
		"s+": this.getSeconds(), //秒
		"q+": Math.floor((this.getMonth() + 3) / 3), //季度
		"S": this.getMilliseconds() //毫秒
	};
	if (/(y+)/.test(fmt))
		fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	for (var k in o)
		if (new RegExp("(" + k + ")").test(fmt))
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
	return fmt;
};

function concatRequestUrl(data) {
	if (typeof data === 'string' && data.indexOf('http://') == -1 && data.indexOf('https://') == -1) {
		return `${config.requestUrl}${data}`
	}
	return data
}

